var uiSingleSelectId = Date.parse(new Date());
var ui_single_select = {};
ui_single_select.analyticalSelect = function(jObj,dataJson){
	if(jObj && jObj.length>0){
		var uiSingleSelect = $('<div></div>').hover(function(){
			$(this).addClass('ui-single-select-mouseover');	
		},function(){
			$(this).removeClass('ui-single-select-mousedown').removeClass('ui-single-select-mouseover');		
		}).mousedown(function(){
			$(this).addClass('ui-single-select-mousedown');		
		}).mouseup(function(){
			$(this).removeClass('ui-single-select-mousedown');			
		}).addClass('ui-single-select');
		var uiSingleSelectSpan = $('<span></span>').text(jObj.attr('desc')).appendTo(uiSingleSelect);
		var uiSingleSelectB = $('<b></b>').appendTo(uiSingleSelect);
		jObj.after(uiSingleSelect);
		
		var uiSingleSelectOptions = ui_single_select.analyticalJson(jObj,uiSingleSelect,dataJson);
		//开启弹出框事件
		uiSingleSelect.click(function(){
			var offset = $(this).offset();
			//计算弹出框的左
			var x = offset.left+$(this).width()+parseInt($(this).css('padding-left').replace('px',''))+parseInt($(this).css('padding-right').replace('px',''));
			var y = offset.top+$(this).height();
			
			uiSingleSelectOptions.css({
				'zIndex':10000,
				'opacity':0,
				'left':(x-uiSingleSelectOptions.width()+1)+'px',
				'top':(y)+'px'
			}).show().animate({ 
				'left':(x-uiSingleSelectOptions.width()+1)+'px',
				'top':(y+4)+'px',
				opacity:1
			}, 500);
		});
		
		return {
			restore:function(){
				uiSingleSelectSpan.text(jObj.attr('desc'));	
				jObj.val(jObj.attr('defaultId'));
			}
		}
	}
}

/**
**
.ui-single-select－options{padding:8px 0;border:1px solid #999999;position:absolute;top:32px;background-color:#FFFFFF;border-radius:4px;box-shadow:0 0 10px rgba(0, 0, 0, 0.5);cursor:default;z-index:130;left:252px;top:88px;display:none;}
.ui-single-select－options .menuitem{padding-left:40px;height:24px;line-height:24px;width:108px;position:relative;}
.ui-single-select－options .menuitem .icon{position:absolute;width:15px;height:15px;top:4px;left:20px;}
.ui-single-select－options .menuitem .close{background:url(../images/btn-bg.png) -31px -318px no-repeat transparent;}
.ui-single-select－options .mouseover{background-color:#3366CC;color:#FFFFFF;}
.ui-single-select－options .dividing-line{background-color:#E9E9E9;margin:4px 0;font-size:0;line-height:0;height:1px;}
**
[{
	id:10000000,
	name:'所有状态',
	type:'item',
	callback:function(){}
},{
	id:10000001,
	name:'未阅读',
	type:'item'	
}]
**
**/
//解析json数据
ui_single_select.analyticalJson = function(jObj,uiSingleSelect,dataJson){
	//弹出层
	var uiSingleSelectOptions = $('<div></div>').hide().addClass('ui-single-select-options').appendTo(window.document.body);
	var uiSingleSelectOptionArray = new Array();
	for(var i = 0;i<dataJson.length;i++){
		if(dataJson[i].type=='item'){
			var uiSingleSelectOption = null;
			if(dataJson[i].id)
				uiSingleSelectOption = $('<div></div>').text(dataJson[i].name).addClass('option').attr('optionId',dataJson[i].id).appendTo(uiSingleSelectOptions);	
			else
				uiSingleSelectOption = $('<div></div>').text(dataJson[i].name).addClass('option').appendTo(uiSingleSelectOptions);	
			uiSingleSelectOption.attr('i',i);
			uiSingleSelectOptionArray.push(uiSingleSelectOption);
			uiSingleSelectOptionArray[i].click(function(){
				if(dataJson[$(this).attr('i')].callback){
					dataJson[$(this).attr('i')].callback();	
				}
				if($(this).attr('optionId'))
					jObj.val($(this).attr('optionId'));
				uiSingleSelect.find('span').text(dataJson[$(this).attr('i')].name);
				uiSingleSelectOptions.hide();
			}).hover(function(){
				$(this).addClass('mouseover');
			},function(){
				$(this).removeClass('mouseover');	
			});
		}	
		else if(dataJson[i].type=='line')
			$('<div></div>').addClass('dividing-line').appendTo(uiSingleSelectOptions);	
	}
	return uiSingleSelectOptions;
}

$(function(){
	//关闭自由弹出层开启事件
	$(window.document.body).mousedown(function(e){
		var target = $(e.target);
		var result = false;
		for(var i=0;i<6;i++){
			if(target.hasClass('ui-single-select-options')){
				result = true;
				break;
			}else{
				target = target.parent();	
			}
		}
		if(!result)
			$('.ui-single-select-options').hide();
	});	
	
	$('#secContentPage').load(function(){ //确保子页面完成加载
		$('iframe').contents().find('body').click(function(){
			$('.ui-single-select-options').hide();
		});
		//$('body',window.frames["secContentPage"].document).click(function(){
		//	alert(123);	
		//});
	});
});